#include"stdafx.h"

using namespace std;

int Inp_croprot::set_input(
		list2d *croplist,
		string sitename,
		map <string,double> &plotprops)
{
	
	if (CONT["READ_FROM_FILE"]!=0){
		if(set_croprot_from_file(timint_mod)) err::err2("plot::read_croprot: could not read croprot from file");
		else return 0;
	}
	if (set_croprot_from_croplist2D(croplist,timint_mod,sitename)){
		if (set_croprot_from_plotprops(plotprops,croplist,timint_mod,sitename)){
			err::err3("plot::read_croprot:Coud not at all read croprotinformation: plot:" +name);
			return 1;
		}
	}
	return 0;
}
int Inp_croprot::set_croprot_from_file(time_intervall timint_mod)
{
	if(this->ptr->set_read_from_file(root,name,suffix,this->ptr->intervall_mode,this->ptr->multipli,
		this->ptr->dur)){
		return 1;
	}
	variables=this->ptr->get_valkeys();
	return 0;
}
int Inp_croprot::set_croprot_from_croplist2D(list2d *croplist,time_intervall timint_mod,
	string sitename)
	{
			
		if (Rotation_Reader::read_croprot_from_croprot(croplist,*this->ptr,timint_mod.get_start(),
		timint_mod.get_end(),sitename)) return 1;
		this->ptr->print_in_file_simple_style_with_mydate(root,name,suffix);
		return 0;
	}
int Inp_croprot::set_croprot_from_plotprops(map<string,double> &plotprops,list2d *croplist,
	time_intervall timint_mode,string sitename)
{
	int croptype1,croptype2;
	double dur=0.00001;
	timestamp start,end;
	time_intervall tiin1,tiin2;
	croprotentry dummy;

	start.set((int) plotprops["startyear"],(int) plotprops["startmon"],plotprops["startday"]);
	end.set((int) plotprops["endyear"],(int) plotprops["endmon"],plotprops["endday"]);

	// Getting the cropids of recent and past crop
	int recent_crop= (int) plotprops["recent_crop"];
	int previous_crop= (int) plotprops["previous_crop"];

	vector <string> row_id;
	vector <string> var_id;
	var_id=croplist->var_id;

	if (recent_crop<0){
		err::err3 ("plot::set_croprot_from_plotprops: No recent crop defined:"+name);
		return 1;
	}
	row_id.push_back(sitename);
	if(previous_crop>=0) row_id.push_back(sitename);

	//generating a site specific croplist with 1 or two rows
		list2d *plotlist=new list2d(row_id,var_id,croplist->nodata);

	// reading seeding,harvest,croptype for the recent crop
		tiin1=Rotation_Reader::get_time_intervall_of_growth (recent_crop,start,end,croptype1);
		if (croptype1==3){
			if(previous_crop>=0) plotlist->pop_back();
			previous_crop=-999999;
			tiin1=timint_mod;
		}
		//writing first line of plotlist
		plotlist->var["year_seed"][0]=tiin1.get_start().get_year();
		plotlist->var["mon_seed"][0]=tiin1.get_start().get_mon();
		plotlist->var["day_seed"][0]=tiin1.get_start().get_day();
		plotlist->var["year_harvest"][0]=tiin1.get_end().get_year();
		plotlist->var["mon_harvest"][0]=tiin1.get_end().get_mon();
		plotlist->var["day_harvest"][0]=tiin1.get_end().get_day();
		plotlist->var["crop_typ"][0]=croptype1;
		plotlist->var["crop_id"][0]=recent_crop;
		plotlist->var["site_id"][0]=plotprops["site_id"];
	// if the recent crop is no perennial then get info for second crop
	if (previous_crop>=0){
		tiin2=Rotation_Reader::get_time_intervall_of_growth (previous_crop,start,end,croptype2);
		while(tiin2>=tiin1){
			tiin2.set_move_year(-1);
		}
		plotlist->var["year_seed"][1]=tiin2.get_start().get_year();
		plotlist->var["mon_seed"][1]=tiin2.get_start().get_mon();
		plotlist->var["day_seed"][1]=tiin2.get_start().get_day();
		plotlist->var["year_harvest"][1]=tiin2.get_end().get_year();
		plotlist->var["mon_harvest"][1]=tiin2.get_end().get_mon();
		plotlist->var["day_harvest"][1]=tiin2.get_end().get_day();
		plotlist->var["crop_typ"][1]=croptype2;
		plotlist->var["crop_id"][1]=recent_crop;
		plotlist->var["site_id"][1]=plotprops["site_id"];
	}

	this->set_croprot_from_croplist2D(plotlist,timint_mode,sitename);
	return 0;
}